package com.etlmaster.executor.steps.file;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;

import com.etlmaster.executor.Step;
import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.processors.File.FileHelper;
import com.etlmaster.executor.utils.DpVfsUtils;
import com.etlmaster.executor.utils.StepState;

/**
 * @author Wugf
 * @date 2016-03-08
 */
@StepInstruction("fileDelete")
public class FileDeleteStep extends Step {

	private FileDeleteStepMeta meta;

	public FileDeleteStep(StepMeta meta) {
		super(meta);
		this.meta = (FileDeleteStepMeta) meta;
	}

	@Override
	protected int handle() {
		String fileUri = meta.getFileURI();
		FileObject file = null;
		try {
			file = DpVfsUtils.getInstance().resolveFile(fileUri);
			boolean isSuccess = FileHelper.deleteFileObject(file);
			if (isSuccess) {
				meta.log("INFO", "删除文件：{}成功", fileUri);
				return StepState.RUNNING_SUCCESSED;
			} else {
				meta.log("INFO", "删除文件：{}失败", fileUri);
				return StepState.RUNNING_FAILED;
			}
		} catch (FileSystemException e) {
			meta.log("ERROR", "删除文件{}失败,{}", fileUri, e.getMessage());
			return StepState.RUNNING_FAILED;
		} finally {
			DpVfsUtils.getInstance().closeFileObject(file);
		}

	}

	@Override
	public boolean stop() {
		return false;
	}
}
